package awk.script.javassist.field02;


import com.alibaba.ttl.threadpool.TtlExecutors;
import javassist.*;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author yujianlong
 * @create 2021-03-21 00:59
 **/
public class Test11 {

//	private static final TransmittableThreadLocal context = new TransmittableThreadLocal();
//
//	static {
//		context = new ThreadLocal();
//	}



//	static TransmittableThreadLocal<String> threadLocal = new TransmittableThreadLocal<>();
	static ThreadLocal<String> threadLocal = new ThreadLocal<>();
	static ExecutorService pool = TtlExecutors.getTtlExecutorService(Executors.newFixedThreadPool(5));

	public static void main3(String[] args) {
		//传递值
		threadLocal.set("java " );

		pool.execute(new Thread(
				new Runnable() {

					@Override
					public void run() {
						//获取值
						System.out.println(Thread.currentThread().getName() + " 获取透传的值:" + threadLocal.get());
					}
				}
		));


//		for (int i = 0; i < 100; i++) {
//			int index = i;
//			pool.execute(new Thread(new Runnable() {
//
//				@Override
//				public void run() {
//
//					//传递值
//					threadLocal.set("java " + index);
//
//					pool.execute(new Thread(
//							new Runnable() {
//
//								@Override
//								public void run() {
//									//获取值
//									System.out.println(Thread.currentThread().getName() + " 获取透传的值:" + threadLocal.get());
//								}
//							}
//					));
//				}
//			}));
//		}
//
//		try {
//			Thread.sleep(1000);
//			pool.shutdown();
//		} catch (InterruptedException e) {
//			e.printStackTrace();
//		}


	}

	public static void main4(String[] args) throws NotFoundException, CannotCompileException, IOException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException, NoSuchFieldException {
		ClassPool classPool = ClassPool.getDefault();
		classPool.importPackage("com.alibaba.ttl.TransmittableThreadLocal");
		//创建类 用makeClass
		CtClass ctClass = classPool.get("cn.xdf.etp.ztapi.utils.ZTAPIContextHolder");



		CtField para = ctClass.getDeclaredField("context");
		para.setType(classPool.get("com.alibaba.ttl.TransmittableThreadLocal"));

//		para.setName("contextBak");

//		ctClass.removeField(para);

//		para.setGenericSignature("<HashMap<String, String>>");

//		ctClass.addField(CtField.make("private static final TransmittableThreadLocal/*<HashMap<String, String>>*/ context = new TransmittableThreadLocal/*<>*/();", ctClass));
//
//		ctClass.addField(CtField.make("public  static final int  age = 5;", ctClass));

		ctClass.writeFile("/Users/yujianlong/Movies/test3/testClass08");


		ZTContHolder.init();

		ZTContHolder.set("aa","sdsds");

		Object aa = ZTContHolder.get("aa");

//		ZTAPIContextHolder.init();
//
//		ZTAPIContextHolder.set("aa","sdsds");
//
//		Object aa = ZTAPIContextHolder.get("aa");



		//传递值
//		ZTAPIContextHolder.set("java " );

		pool.execute(new Thread(
				new Runnable() {

					@Override
					public void run() {
						//获取值
						System.out.println(Thread.currentThread().getName() + " 获取透传的值:" + ZTContHolder.get("aa"));
//						System.out.println(Thread.currentThread().getName() + " 获取透传的值:" + ZTAPIContextHolder.get("aa"));
					}
				}
		));


//		TtlRunnable ttlRunnable = TtlRunnable.get(() -> {
//			Object aa1 = ZTAPIContextHolder.get("aa");
//			System.out.println(aa1);
//		});
//
//		ExecutorService executorService = Executors.newCachedThreadPool();
//
//		executorService.submit(ttlRunnable);
//				.submit(ttlRunnable);

//		CompletableFuture.runAsync(()->{
//			Object aa1 = ZTAPIContextHolder.get("aa");
//			System.out.println(aa1);
//		});

//		System.out.println(aa);

//
//		Class clazz = ctClass.toClass();
//
//		Field context = clazz.getDeclaredField("context");



	}


	public static void main8(String[] args) throws NotFoundException, CannotCompileException, IOException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException, NoSuchFieldException {
		ClassPool classPool = ClassPool.getDefault();
		classPool.importPackage("com.alibaba.ttl.TransmittableThreadLocal");
		classPool.importPackage("java.util.HashMap");
		classPool.importPackage("java.util.Map");
		//创建类 用makeClass
		CtClass ctClass = classPool.get("cn.xdf.etp.ztapi.utils.ZTAPIContextHolder");



//		String genericSignature = ctClass.getGenericSignature();
//
//		CtField para = ctClass.getDeclaredField("context");
//		para.setModifiers(Modifier.PUBLIC+Modifier.STATIC);
//
//		para.setAttribute();

//		Object constantValue = para.getConstantValue();
//		ctClass.removeField(para);
//		para.setModifiers(Modifier.PUBLIC+Modifier.STATIC);

//		para.se

		CtField param = new CtField(classPool.get("com.alibaba.ttl.TransmittableThreadLocal"), "newContext", ctClass);
		param.setGenericSignature("Lcom/alibaba/ttl/TransmittableThreadLocal<Ljava/util/HashMap<Ljava/lang/String;Ljava/lang/String;>;>;");

		param.setModifiers(Modifier.PRIVATE+Modifier.FINAL+Modifier.STATIC);
		ctClass.addField(param, CtField.Initializer.byNew(classPool.get("com.alibaba.ttl.TransmittableThreadLocal")));

		CtMethod init = ctClass.getDeclaredMethod("init", new CtClass[]{});
		init.setBody("{\n" +
				"        clear();\n" +
				"        newContext.set(new HashMap());\n" +
				"    }");


		CtMethod clear = ctClass.getDeclaredMethod("clear", new CtClass[]{});
		clear.setBody("{\n" +
				"        HashMap/*<String, String>*/ tmp = (HashMap)context.get();\n" +
				"        if (tmp != null) {\n" +
				"            newContext.set((Object)null);\n" +
				"            tmp.clear();\n" +
				"        }\n" +
				"\n" +
				"    }");

		CtMethod data = ctClass.getDeclaredMethod("data", new CtClass[]{});
		data.setBody("{\n" +
				"        HashMap/*<String, String>*/ tmp = (HashMap)newContext.get();\n" +
				"        return null == tmp ? new HashMap() : tmp;\n" +
				"    }");

		CtMethod get = ctClass.getDeclaredMethod("get", new CtClass[]{classPool.get("java.lang.String")});
		get.setBody("{\n" +
				"        if (null == newContext.get()) {\n" +
				"            init();\n" +
				"        }\n" +
				"\n" +
				"        return ((HashMap)newContext.get()).get($1);\n" +
				"    }");

		CtMethod getOrDefault = ctClass.getDeclaredMethod("getOrDefault", new CtClass[]{classPool.get("java.lang.String"),classPool.get("java.lang.Object")});
		getOrDefault.setBody("{\n" +
				"        Object rtn = get($1);\n" +
				"        return rtn == null ? $2 : rtn;\n" +
				"    }");

//
		CtMethod set1 = ctClass.getDeclaredMethod("set", new CtClass[]{classPool.get("java.util.Map")});
		set1.setBody("{\n" +
				"        ((HashMap)newContext.get()).putAll($1);\n" +
				"    }");

		CtMethod set2 = ctClass.getDeclaredMethod("set", new CtClass[]{classPool.get("java.lang.String"),classPool.get("java.lang.Object")});
		set2.setBody("{\n" +
				"        ((HashMap)newContext.get()).put($1, String.valueOf($2));\n" +
				"    }");



//		ctClass.removeField(para);
//		ctClass.addField(CtField.make("private static final TransmittableThreadLocal/*<HashMap<String, String>>*/ context = new TransmittableThreadLocal/*<>*/();", ctClass));
//		para.
//
////		CtField.Initializer.
//
//		String signature = para.getSignature();
//		String genericSignature1 = para.getGenericSignature();//Ljava/lang/ThreadLocal<Ljava/util/HashMap<Ljava/lang/String;Ljava/lang/String;>;>;
//
//		CtClass type = para.getType();
//
//		FieldInfo fieldInfo = para.getFieldInfo();
//
//		para.setGenericSignature("Lcom/alibaba/ttl/TransmittableThreadLocal<Ljava/util/HashMap<Ljava/lang/String;Ljava/lang/String;>;>;");
////		para.setName("context");
//		para.setType(classPool.get("com.alibaba.ttl.TransmittableThreadLocal"));


//		para.setName("contextbak");
//		para.

//		ctClass.addField(para);

//		para.setName("contextBak");

//		ctClass.removeField(para);

//		para.setGenericSignature("<HashMap<String, String>>");

//		ctClass.addField(CtField.make("private static final TransmittableThreadLocal/*<HashMap<String, String>>*/ context = new TransmittableThreadLocal/*<>*/();", ctClass));
//
//		ctClass.addField(CtField.make("public  static final int  age = 5;", ctClass));

		ctClass.writeFile("/Users/yujianlong/Movies/test3/testClass08");




	}


	public static void main9(String[] args) throws NotFoundException, CannotCompileException, IOException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException, NoSuchFieldException {
		ClassPool classPool = ClassPool.getDefault();
		classPool.importPackage("com.alibaba.ttl.TransmittableThreadLocal");
		classPool.importPackage("java.util.HashMap");
		classPool.importPackage("java.util.Map");
		//创建类 用makeClass
		CtClass ctClass = classPool.get("cn.xdf.etp.ztapi.utils.ZTAPIContextHolder");


		CtField param = new CtField(classPool.get("com.alibaba.ttl.TransmittableThreadLocal"), "newContext", ctClass);
		param.setGenericSignature("Lcom/alibaba/ttl/TransmittableThreadLocal<Ljava/util/HashMap<Ljava/lang/String;Ljava/lang/String;>;>;");

		param.setModifiers(Modifier.PRIVATE+Modifier.FINAL+Modifier.STATIC);
		ctClass.addField(param, CtField.Initializer.byNew(classPool.get("com.alibaba.ttl.TransmittableThreadLocal")));

		CtMethod init = ctClass.getDeclaredMethod("init", new CtClass[]{});
		init.setBody("{\n" +
				"        clear();\n" +
				"        newContext.set(new HashMap());\n" +
				"    }");


		CtMethod clear = ctClass.getDeclaredMethod("clear", new CtClass[]{});
		clear.setBody("{\n" +
				"        HashMap/*<String, String>*/ tmp = (HashMap)context.get();\n" +
				"        if (tmp != null) {\n" +
				"            newContext.set(null);\n" +
				"            tmp.clear();\n" +
				"        }\n" +
				"\n" +
				"    }");

		CtMethod data = ctClass.getDeclaredMethod("data", new CtClass[]{});
		data.setBody("{\n" +
				"        HashMap/*<String, String>*/ tmp = (HashMap)newContext.get();\n" +
				"        return null == tmp ? new HashMap() : tmp;\n" +
				"    }");

		CtMethod get = ctClass.getDeclaredMethod("get", new CtClass[]{classPool.get("java.lang.String")});
		get.setBody("{\n" +
				"        if (null == newContext.get()) {\n" +
				"            init();\n" +
				"        }\n" +
				"\n" +
				"        return ((HashMap)newContext.get()).get($1);\n" +
				"    }");

		CtMethod getOrDefault = ctClass.getDeclaredMethod("getOrDefault", new CtClass[]{classPool.get("java.lang.String"),classPool.get("java.lang.Object")});
		getOrDefault.setBody("{\n" +
				"        Object rtn = get($1);\n" +
				"        return rtn == null ? $2 : rtn;\n" +
				"    }");

//
		CtMethod set1 = ctClass.getDeclaredMethod("set", new CtClass[]{classPool.get("java.util.Map")});
		set1.setBody("{\n" +
				"        ((HashMap)newContext.get()).putAll($1);\n" +
				"    }");

		CtMethod set2 = ctClass.getDeclaredMethod("set", new CtClass[]{classPool.get("java.lang.String"),classPool.get("java.lang.Object")});
		set2.setBody("{\n" +
				"        ((HashMap)newContext.get()).put($1, String.valueOf($2));\n" +
				"    }");


		ctClass.writeFile("/Users/yujianlong/Movies/test3/testClass08");


	}

	public static void main(String[] args) throws NotFoundException, CannotCompileException, IOException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException, ClassNotFoundException, NoSuchFieldException {
		ClassPool classPool = ClassPool.getDefault();
		classPool.importPackage("com.alibaba.ttl.TransmittableThreadLocal");
		classPool.importPackage("java.util.HashMap");
		classPool.importPackage("java.util.Map");
		//创建类 用makeClass
		CtClass ctClass = classPool.get("cn.xdf.etp.ztapi.utils.ZTAPIContextHolder");

//		CtField context = ctClass.getDeclaredField("context");
//
//		context.setName("context2");



//
//
//		context.setGenericSignature("Lcom/alibaba/ttl/TransmittableThreadLocal<Ljava/util/HashMap<Ljava/lang/String;Ljava/lang/String;>;>;");
//


		CtField param = new CtField(classPool.get("com.alibaba.ttl.TransmittableThreadLocal"), "context", ctClass);
		param.setGenericSignature("Lcom/alibaba/ttl/TransmittableThreadLocal<Ljava/util/HashMap<Ljava/lang/String;Ljava/lang/String;>;>;");

		param.setModifiers(Modifier.PRIVATE+Modifier.FINAL+Modifier.STATIC);
		ctClass.addField(param, CtField.Initializer.byNew(classPool.get("com.alibaba.ttl.TransmittableThreadLocal")));



		ctClass.writeFile("/Users/yujianlong/Movies/test3/testClass08");

	}


}
